package com.example.zyfypt_611cgt.activities;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;

import com.example.zyfypt_611cgt.R;
import com.example.zyfypt_611cgt.fragments.BaseFragment;
import com.example.zyfypt_611cgt.fragments.Fragment1;
import com.example.zyfypt_611cgt.fragments.Fragment2;
import com.example.zyfypt_611cgt.fragments.Fragment3;
import com.example.zyfypt_611cgt.fragments.Fragment4;
import com.example.zyfypt_611cgt.fragments.Fragment5;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<BaseFragment> fragmentList;
    private ViewPager viewPager;
    private BottomNavigationView bottomnv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFragmentList();
        initViewPager();
        initBottomNV();
    }

    private void initBottomNV() {
        bottomnv = findViewById(R.id.bottomnv);
        bottomnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int vpid = 0;

                switch(item.getItemId()){
                    case R.id.blog: vpid = 0;break;
                    case R.id.keynote: vpid = 1;break;
                    case R.id.video: vpid = 2;break;
                    case R.id.sample: vpid = 3;break;
                    case R.id.owner: vpid = 4;break;
                }
                viewPager.setCurrentItem(vpid);
                return true;
            }
        });
    }

    private void initViewPager() {
        viewPager = findViewById(R.id.viewpager);
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };

        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch(position){
                    case 0: bottomnv.setSelectedItemId(R.id.blog);
                        break;
                    case 1: bottomnv.setSelectedItemId(R.id.keynote);
                        break;
                    case 2: bottomnv.setSelectedItemId(R.id.video);
                        break;
                    case 3: bottomnv.setSelectedItemId(R.id.sample);
                        break;
                    case 4: bottomnv.setSelectedItemId(R.id.owner);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initFragmentList() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());
        fragmentList.add(new Fragment4());
        fragmentList.add(new Fragment5());

    }
}